<?php
require_once "utils/db.php";
require_once "utils/util.php";
$nodes = array();
if($_POST['node'] == 0){
	$query = 'SELECT * FROM entities ORDER BY name;';
	$db = new db();
		$result = $db -> rawSelect($query);
	$db->close();
	foreach ($result as $row) {
		$path['text']		= $row['plural_label'];
		$path['id']			= $row['id'];
		$path['leaf']		= false;
		$path['cls']		= 'file';
		$nodes[] = $path;
	}
}else{
	$query = "SELECT * FROM entity_fields WHERE entity_id = '".$_POST['node']."' ORDER BY form_location;";
	$db = new db();
		$result = $db -> rawSelect($query);
	$db->close();
	foreach ($result as $row) {
		$path['text']		= $row['name'];
		$path['id']			= $row['id'];
		$path['leaf']		= true;
		$path['cls']		= 'file';
		$nodes[] = $path;
	}
}

print json_encode($nodes);
?>